/*
 * @Author: zxc
 * @Date: 2020-07-28 14:33:12
 * @LastEditTime: 2020-09-07 14:38:36
 * @LastEditors: zxc
 * @Description: 导航菜单相关
 */

import { menuAction } from "../../../saga/actions/menu";
import { recursiveMenu } from "./utils";
import LocalStores from "../../../../utils/LocalStore";

const initalStateSetter: IMenu = {
  breadcrumb: {},
  topMenu: [],
  sideMenu: {},
  currentSidebar: [],
  currentTopMenu: null,
  theme: LocalStores.get("theme") || "dark",
  primaryColor: LocalStores.get("primaryColor") || "#d214a2",
  drawer: false,
};

export default function (state = initalStateSetter, action: ActionParams) {
  switch (action.type) {
    case menuAction.SET_MENU: {
      const { routes } = action.payload;
      const { topMenu, breadcrumb, sideMenu } = recursiveMenu(routes);
      return {
        ...state,
        topMenu,
        breadcrumb,
        sideMenu,
      };
    }
    case menuAction.SET_CURRENT_MENU: {
      return {
        ...state,
        ...action.payload,
        currentSidebar: state.sideMenu[action.payload.currentTopMenu] || [],
      };
    }

    case menuAction.SET_DRAWER: {
      return {
        ...state,
        drawer: action.payload,
      };
    }

    case menuAction.SET_THEME: {
      // 存入 本地存储
      LocalStores.set("theme", action.payload.theme);
      return {
        ...state,
        ...action.payload,
      };
    }

    case menuAction.SET_PRIMARY_COLOR: {
      // 存入 本地存储
      LocalStores.set("primary-color", action.payload);

      return {
        ...state,
        primaryColor: action.payload,
      };
    }
    // case ASYNC_SET_MENU:
    //   console.log('一个异步请求来了',action)
    //   return {
    //     ...state
    //   }
    default:
      return state;
  }
}
